package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.form.ApplyRefundForm;
import com.zimmur.app.mocolink.domain.form.ShipOrderForm;
import com.zimmur.app.mocolink.domain.model.Order;
import com.zimmur.app.mocolink.domain.model.OrderDetail;
import com.zimmur.app.mocolink.domain.param.OrderParamForManage;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.OrderInfo;
import com.zimmur.app.mocolink.domain.vo.OrderVoForManage;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IOrderService;
import com.zimmur.app.mocolink.service.IRefundOrderService;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IRefundOrderService refundOrderService;
	
	@RequestMapping("/index")
	public String index(ModelMap model){
		OrderParamForManage param = new OrderParamForManage(1);
		PageUtil<OrderInfo> page = orderService.selectByParam(param);
		model.addAttribute("page", page);
		model.addAttribute("param", param);
		return "center/order/list";
	}
	@RequestMapping("/query")
	public String query(ModelMap model){
		Integer pageNo = getParam("pageNo", 1);
		String status = getParam("status");
		String keyword = getParam("keyword");
		String startTime = getParam("startTime");
		String endTime = getParam("endTime");
		Integer goodsNumMin = getIntParam("goodsNumMin");
		Integer goodsNumMax = getIntParam("goodsNumMax");
		Integer payJeMin = getIntParam("payJeMin");
		Integer payJeMax = getIntParam("payJeMax"); 
		OrderParamForManage param = new OrderParamForManage(pageNo, status, keyword, startTime, endTime, goodsNumMin, goodsNumMax, payJeMin, payJeMax);
		PageUtil<OrderInfo> page = orderService.selectByParam(param);
		model.addAttribute("page", page);
		model.addAttribute("param", param);
		return "center/order/list";
	}
	@RequestMapping("/showDetail")
	public String showDetail(ModelMap model){
		Integer orderId = getParam("orderId", 0);
		OrderVoForManage orderVo = orderService.selectOrderDetail(orderId);
		if(orderVo!=null){
			model.addAttribute("orderVo", orderVo);
		}
		return "center/order/detail";
	}
	@ResponseBody
	@RequestMapping("/payOrder")
	public ReturnJsonEntity payOrder(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer orderId = getParam("orderId", 0);
		String username = getUsername();
		int flag = orderService.payOrder(orderId, username);
		if(flag>0){
			codeEnum=StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum=StatusCodeEnum.CODE410401;
		}else if(flag == -2){
			codeEnum=StatusCodeEnum.CODE410402;
		}else if(flag == -3){
			codeEnum=StatusCodeEnum.CODE410403;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/sendGoods")
	public String sendGoods(ModelMap model){
		Integer orderId = getParam("orderId", 0);
		model.addAttribute("orderId", orderId);
		return "center/order/shipInfo";
	}
	@RequestMapping("/refundOrder")
	public String refundOrder(ModelMap model){
		Integer orderId = getParam("orderId", 0);
		OrderVoForManage orderVo = orderService.selectOrderDetail(orderId);
		if(orderVo!=null){
			String status = orderVo.getOrder().getStatus();
			if(Constant.Order_Status_Wait_Send.equals(status)
				|| Constant.Order_Status_Wait_Sign.equals(status)
				|| Constant.Order_Status_Wait_Comment.equals(status)
				|| Constant.Order_Status_Done.equals(status)){
				model.addAttribute("orderVo", orderVo);
			}
		}
		return "center/order/refundApply";
	}
	@ResponseBody
	@RequestMapping("/saveShip")
	public ReturnJsonEntity saveShip(ShipOrderForm form){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		if(StringUtils.isBlank(form.getCompanyName(),form.getShipDate(),form.getShipNo())){
			codeEnum = StatusCodeEnum.CODE100001;
		}else{
			int flag = orderService.saveShipInfo(form);
			if(flag>0){
				codeEnum=StatusCodeEnum.CODE000000;
			}else if(flag==-1){
				codeEnum = StatusCodeEnum.CODE410401;
			}else if(flag == -2){
				codeEnum = StatusCodeEnum.CODE410404;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/updateStatus")
	public ReturnJsonEntity updateStatus(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer orderId = getParam("orderId", 0);
		String status = getParam("status");
		int flag =orderService.updateOrderStatus(orderId, status);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE410401;
		}
		entity.init(codeEnum);
		return entity;
	}
	
	@ResponseBody
	@RequestMapping("/saveRefund")
	public ReturnJsonEntity saveRefund(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String json = getParam("applyInfo");
		if(!StringUtils.isBlank(json)){
			ApplyRefundForm form=JSONObject.parseObject(json, ApplyRefundForm.class);
			
			int flag = refundOrderService.saveRefundInfo(form);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum = StatusCodeEnum.CODE410401;
			}else if(flag == -2){
				codeEnum = StatusCodeEnum.CODE410405;
			}
		}else{
			codeEnum = StatusCodeEnum.CODE100001;
		}
		
		entity.init(codeEnum);
		return entity;
	}
}
